package com.itheima.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.dto.RecommendProduct;
import com.itheima.dto.RecommendSubject;
import com.itheima.excption.MyException;
import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.pojo.SmsHomeRecommendProduct;
import com.itheima.pojo.SmsHomeRecommendSubject;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SmsHomeRecommendProductService {

    @DubboReference
    private SmsHomeRecommendProductApi smsHomeRecommendProductApi;
    /**
     * 添加首页推荐
     *
     * @param
     */
    public void createProduct(List<RecommendProduct> recommendProducts) {
        for (RecommendProduct recommendProduct : recommendProducts) {

            SmsHomeRecommendProduct smsHomeRecommendProduct = new SmsHomeRecommendProduct();
            if (recommendProduct.getId() != null) {
                smsHomeRecommendProduct.setId(recommendProduct.getId().longValue());
            }
            if (recommendProduct.getProductId() != null) {
                smsHomeRecommendProduct.setProductId(recommendProduct.getProductId().longValue());
            }
            smsHomeRecommendProduct.setProductName(recommendProduct.getProductName());
            smsHomeRecommendProduct.setSort(recommendProduct.getSort());
            smsHomeRecommendProduct.setRecommendStatus(recommendProduct.getRecommendStatus());
            //根据商品的id 查询 是否已经被推荐 ,如果已经被推荐 则提示
            SmsHomeRecommendProduct smsHomeRecommendProductFind=smsHomeRecommendProductApi.findByPid(recommendProduct.getProductId());
            if(!ObjectUtils.isEmpty(smsHomeRecommendProductFind)){
                //已经被推荐
                throw  new MyException("推荐失败!该商品已经被推荐,请前往列表查看");
            }
            //执行数据库的保存
            smsHomeRecommendProductApi.save(smsHomeRecommendProduct);
            //  TODO 是否需要对id重复进行校验? 不用

        }


    }

    /**
     * 批量删除
     * @param ids
     */
    public void delete(Integer[] ids) {
        //删除之前,先进行判断选中的id 是否 有启用状态的
        for (Integer id : ids) {
            SmsHomeRecommendProduct byID = smsHomeRecommendProductApi.findByID(id);
            if(byID.getRecommendStatus()==1){
                throw new MyException("删除失败,请先修改状态,再进行删除");
            }
        }
        smsHomeRecommendProductApi.deleteByIds(ids);

    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @param subjectName
     * @return
     */
    public PageResultVo<RecommendProduct> list(Integer pageNum, Integer pageSize, Integer recommendStatus, String subjectName) {
        IPage<SmsHomeRecommendProduct> page=smsHomeRecommendProductApi.list(pageNum,pageSize,recommendStatus,subjectName);
        //返回回来的page对象
        PageResultVo<RecommendProduct> pageResultVo=new PageResultVo<>();
        pageResultVo.setPageNum(pageNum);
        pageResultVo.setPageSize(pageSize);
        Long total = page.getTotal();
        pageResultVo.setTotal(total.intValue());
        Long pages = page.getPages();
        pageResultVo.setTotalPages(pages.intValue());
        List<RecommendProduct> recommendProductList=new ArrayList<>();

//      BeanUtils.copyProperties(page.getRecords(),recommendSubjectList);
        List<SmsHomeRecommendProduct> records = page.getRecords();
        recommendProductList= records.stream().map((item)->{
            RecommendProduct recommendProduct=new RecommendProduct();
            recommendProduct.setProductId(item.getProductId().intValue());
            recommendProduct.setId(item.getId().intValue());
            recommendProduct.setSort(item.getSort());
            recommendProduct.setRecommendStatus(item.getRecommendStatus());
            recommendProduct.setProductName(item.getProductName());
            return recommendProduct;
        }).collect(Collectors.toList());


        pageResultVo.setList(recommendProductList);
        return pageResultVo;



    }

    /**
     * 批量修改状态
     * @param recommendStatus
     * @param ids
     */
    public void update(Integer recommendStatus, Integer[] ids) {
        smsHomeRecommendProductApi.updateByIds(recommendStatus,ids);
    }


    /**
     * 修改排序值
     * @param id
     * @param sort
     */
    public void updateSort(Integer id, Integer sort) {
        SmsHomeRecommendProduct smsHomeRecommendProduct=new SmsHomeRecommendProduct();
        smsHomeRecommendProduct.setSort(sort);
        smsHomeRecommendProduct.setId(id.longValue());
        smsHomeRecommendProductApi.update(smsHomeRecommendProduct);

    }



}
